//
//  ChooseColorView.m
//  PWDAssistant
//
//  Created by Ajen on 2018/11/24.
//  Copyright © 2018 Ajen. All rights reserved.
//

#import "ChooseColorView.h"
#define COLOR_HOUSE_ARRAY  @[@"#fcd337",@"#32c027",@"#ff0000",@"#36282b",@"#ec9bad",@"#61ac85",@"#15559a",@"#f26b1f",@"#945833",@"#c35691",@"#b0d5df",@"#869d9d",@"#0eb0c9",@"#856d72",@"#ef82a0",@"#382129",@"#283129",@"#525288",@"#312c33",@"#1677b3"]
@implementation ChooseColorView{
    NSMutableArray * topColorArray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        topColorArray = [NSMutableArray arrayWithCapacity:0];
        UILabel * label = [UILabel new];
        label.aj_Text(@"标记:").aj_font([UIFont systemFontOfSize:15 weight:UIFontWeightMedium]).aj_textColorHexStr(@"#444444").aj_addToView(self);
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(20);
            make.top.equalTo(self).offset(20);
        }];
        
        [self layoutIfNeeded];
        
        for(int i = 0 ; i < 3 ; i++){
            UIImageView * colorBg = [UIImageView new];
            colorBg.userInteractionEnabled = YES;
            colorBg.aj_imgName(@"colorBg").aj_addToView(self);
            colorBg.frame = CGRectMake(20 + i * 35, label.Bottom + 10, 25, 40);
            
            UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.aj_cornerRadius(3.5).aj_addToView(colorBg);
            button.tag = 100 + i;
            [button addTarget:self action:@selector(topButtonDown:) forControlEvents:UIControlEventTouchUpInside];
            button.frame = colorBg.bounds;
            button.alpha = 0;
            button.userInteractionEnabled = NO;
        }
        
        
       
        NSInteger maxItem = 0;//每行的最大值
        for(int i = 0 ; i < COLOR_HOUSE_ARRAY.count ; i++){
            if(i * 35 + 60 > SCREEN_WIDTH && maxItem == 0){
                maxItem = i;
            }
        }
        for(int i = 0 ; i < COLOR_HOUSE_ARRAY.count ; i++){
            UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.aj_buttonTitle(@"+").aj_buttonTintColorHex(@"#ffffff").aj_bgColorHexStr(COLOR_HOUSE_ARRAY[i]).aj_cornerRadius(3.5).aj_addToView(self);
            button.tag = 200 + i;
            [button addTarget:self action:@selector(bottomButtonDown:) forControlEvents:UIControlEventTouchUpInside];
            if(maxItem>0){
                button.frame = CGRectMake(20 + (i%maxItem) * 35, label.Bottom + 65 + (i/maxItem) * 50, 25, 40);
            }else{
                button.frame = CGRectMake(20 + i * 35, label.Bottom + 65, 25, 40);
            }
            
            
        }
    }
    return self;
}
-(void)topButtonDown:(UIButton *)button{
    topColorArray = [topColorArray AJRemoveIndex:button.tag - 100];
    
    UIButton * topButton_0 = (UIButton *)[self viewWithTag:100];
    UIButton * topButton_1 = (UIButton *)[self viewWithTag:101];
    UIButton * topButton_2 = (UIButton *)[self viewWithTag:102];
    
    topButton_0.userInteractionEnabled = NO;
    topButton_1.userInteractionEnabled = NO;
    topButton_2.userInteractionEnabled = NO;
    
    topButton_0.alpha = 0;
    topButton_1.alpha = 0;
    topButton_2.alpha = 0;
    
    NSArray * topButtonArray = @[topButton_0,topButton_1,topButton_2];
    
    if(topColorArray.count < 3){
        for(int i = 0 ; i < topColorArray.count ; i++){
            UIButton * temp = (UIButton *)topButtonArray[i];
            temp.aj_bgColorHexStr(topColorArray[i]);
            temp.alpha = 1;
            temp.userInteractionEnabled = YES;
        }
    }
    if(self.actionBlock){
        self.actionBlock(topColorArray);
    }
}
-(void)bottomButtonDown:(UIButton *)button{
    AJLog(@"%ld",button.tag);
    AJLog(@"%@",topColorArray);
    if(topColorArray.count >= 3){
        return;
    }
    UIButton * topButton_0 = (UIButton *)[self viewWithTag:100];
    UIButton * topButton_1 = (UIButton *)[self viewWithTag:101];
    UIButton * topButton_2 = (UIButton *)[self viewWithTag:102];
    
    topButton_0.userInteractionEnabled = NO;
    topButton_1.userInteractionEnabled = NO;
    topButton_2.userInteractionEnabled = NO;
    
    topButton_0.alpha = 0;
    topButton_1.alpha = 0;
    topButton_2.alpha = 0;
    
    NSArray * topButtonArray = @[topButton_0,topButton_1,topButton_2];
    
    if(topColorArray.count <= 3){
        [topColorArray addObject:COLOR_HOUSE_ARRAY[button.tag - 200]];
        
        for(int i = 0 ; i < topColorArray.count ; i++){
            UIButton * temp = (UIButton *)topButtonArray[i];
            temp.aj_bgColorHexStr(topColorArray[i]);
            temp.alpha = 1;
            temp.userInteractionEnabled = YES;
        }
    }else{
        ShowTextHUD(@"标注位置已满");
    }
    
    if(self.actionBlock){
        self.actionBlock(topColorArray);
    }
}

-(void)clear{
    [topColorArray removeAllObjects];
    
    UIButton * topButton_0 = (UIButton *)[self viewWithTag:100];
    UIButton * topButton_1 = (UIButton *)[self viewWithTag:101];
    UIButton * topButton_2 = (UIButton *)[self viewWithTag:102];
    
    topButton_0.userInteractionEnabled = NO;
    topButton_1.userInteractionEnabled = NO;
    topButton_2.userInteractionEnabled = NO;
    
    topButton_0.alpha = 0;
    topButton_1.alpha = 0;
    topButton_2.alpha = 0;
}

-(void)setLocalColorArray:(NSArray *)localColorArray{
    _localColorArray  = localColorArray;
    topColorArray = [localColorArray mutableCopy];
    UIButton * topButton_0 = (UIButton *)[self viewWithTag:100];
    UIButton * topButton_1 = (UIButton *)[self viewWithTag:101];
    UIButton * topButton_2 = (UIButton *)[self viewWithTag:102];
    
    topButton_0.userInteractionEnabled = NO;
    topButton_1.userInteractionEnabled = NO;
    topButton_2.userInteractionEnabled = NO;
    
    topButton_0.alpha = 0;
    topButton_1.alpha = 0;
    topButton_2.alpha = 0;
    
    NSArray * topButtonArray = @[topButton_0,topButton_1,topButton_2];
    
    for(int i = 0 ; i < topColorArray.count ; i++){
        UIButton * temp = (UIButton *)topButtonArray[i];
        temp.aj_bgColorHexStr(topColorArray[i]);
        temp.alpha = 1;
        temp.userInteractionEnabled = YES;
    }
}
@end
